<template>
	<view class="quick-login">
		<view class="weixin">
			<button type="primary" size="mini" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号快捷登录</button>
		</view>
		<view class="common">
			<button type="" size="mini" @click="transteCommonLogin()">手机号码登录/注册</button>
		</view>
	</view>
</template>

<script>
	import local_userinfo from '@/util/user/local-userinfo.js'
	export default {
		data() {
			return {
				showQuickPhone: true,
				wxcode: '',
				wantJumpurl:''
			}
		},
		onShow() {
			this.checkLogin()
			this.$setTitle("嗨焙鲜生·登录")
		},
		onLoad(options) {
			this.wantJumpurl = decodeURIComponent(options.redirect || '/pages/tabBar/user');
			// #ifdef MP-WEIXIN
			uni.hideShareMenu()
			// #endif
		},
		methods: {
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let encryptedData = e.detail.encryptedData;
					let iv = e.detail.iv;
					let that =this;
					this.onGet(iv, encryptedData, (tag) => {
						let phoneObj = JSON.parse(tag);
						//区号
						let countryCode = phoneObj.countryCode;

						if (countryCode === '86') {
							let phone = phoneObj.phoneNumber;
							that.toLogin(phone);
						}

					})
				}
			},
			onGet(iv, encryptedData, callback) {
				let _this = this;
				let code = this.wxcode;
				
				uni.showLoading({
					title:'请稍等...',
					mask:true
				})
				//发起网络请求
				this.$request.get({
					data: {
						c: 'LoginController',
						m: 'getPhoneByMP_Weixin',
						code: code,
						IV: iv,
						EncryptedData: encryptedData,
					}
				}).then(res => {
					let result = res;
					if (result.Status == "ok") {
						if (callback) callback(result.Tag)
					}
				}).catch(error => {
					console.log(error)
				})
			},
			getUserPhone(wxcode) {
				console.log(wxcode)
			},
			checkLogin() {
				let that = this;
				uni.login({
					provider: 'weixin',
					timeout: 5000,
					success(loginRes) {
						if (loginRes.errMsg == "login:ok") {
							that.wxcode = (loginRes.code);

						}
					},
					fail() {

					},
					complete(res) {}
				})
			},
			transteCommonLogin() {
				let wantJumpurl =encodeURIComponent(this.wantJumpurl);
				uni.navigateTo({
					url: '../../pages/login/common?redirect='+wantJumpurl
				})
			},
			toLogin: function(phone) {
				let _this = this;
				uni.showLoading({
					title:'请稍等...',
					mask:true
				})
				this.$request.get({
					data: {
						c: 'LoginController',
						m: 'login_Authorize',
						phone: phone,
					}
				}).then(res => {
					console.log(res)
					let data = res;
					if (data.Status === 'ok') {

						let user = {};
						user.userid = data.Tag.Id;
						user.sessionid = data.Tag.rediskey;
						user.jzb = data.Tag.Money;
						user.phone = data.Tag.Mobile;
						user.vip = data.Tag.Vip;
						user.vipexpiretime = data.Tag.VipExpireTime;
						user.headimg = data.Tag.HeadImage;
						user.loginname = data.Tag.LoginName;
						user.realname = data.Tag.RealName;
						
						local_userinfo.setUserinfo(user)
						
						let wantJumpurl = _this.wantJumpurl;
						if (_this.wantJumpurl.indexOf('/pages/tabBar/') > -1) {
							uni.switchTab({
								url: wantJumpurl
							})
						} else {
							uni.redirectTo({
								url: wantJumpurl
							});
						}
					}
				}).catch(error=>{
					console.log(error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.quick-login {
		width: 100%;
		height: 100%;
		position: fixed;
		background: #fff;
		text-align: center;
		z-index: 9;

		.weixin {
			margin-top: 50%;
		}

		button {
			width: 60%;
			margin-bottom: 30upx;
		}
	}
</style>
